app.controller("userdata", ['$scope', '$window', '$stateParams', '$timeout', '$http', '$state', '$cookieStore', 'Md5', 'fileUpload', function ($scope, $window, $stateParams, $timeout, $http, $state, $cookieStore, Md5, fileUpload) {
        
    // 个人中心详细资料

    $scope.login();//判断是否登录
    if ($cookieStore.get('login').id) {
        console.log($cookieStore.get('login').id)
        $http({
            method: 'post',
            url: $scope.httptit + '/index/user/index.html',
            data: {
                do: 'detail',
                userid: $cookieStore.get('login').id,
            },
            dataType: 'json',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest: $scope.changeData
        }).then(function successCallback(response) {
            $scope.userDesc = response.data.data
            console.log($scope.userDesc)
        }, function errorCallback(response) {
            // 请求失败执行代码
        });
    } else {
        $.alert({
            title: '登录',
            text: '请登录后重试',
            onOK: function () {
                //点击确认
            }
        });
    }

    // 点击获取验证码
    $scope.OnClick = '';
    $scope.getCode = function () {
        if ((/^1(3|4|5|7|8)\d{9}$/.test($scope.user_Phone))) {
            if ($scope.OnClick) {
                console.log($scope.OnClick)
                return
            }
            $scope.OnClick = 'disabled';
            $timeout(function () {
                $scope.OnClick = '';
            }, 60000);
            console.log('111')
            //上传号码到服务器接收验证码
            $http({
                method: 'post',
                url: $scope.httptit + '/index/user/sendChit.html',
                data: {
                    userid: $cookieStore.get('login').id,
                    tel: $scope.user_Phone,
                },
                dataType: 'json',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest: $scope.changeData
            }).then(function successCallback(response) {
                console.log(response)
                $cookieStore.put('VERCode', response.data.data)
                if (response.data.status == 101) {
                    // 弹出框提示
                    $.prompt({
                        title: '验证码',
                        text: '请输入接收到的验证码',
                        input: '请输入验证码',
                        empty: false, // 是否允许为空
                        onOK: function (input) {
                            //验证成功后的操作   
                            console.log(input)
                            if (input == $cookieStore.get('VERCode')) {
                                $http({
                                    method: 'post',
                                    url: $scope.httptit + '/index/user/checkChit.html',
                                    data: {
                                        userid: $cookieStore.get('login').id,
                                        tel: $scope.user_Phone,
                                    },
                                    dataType: 'json',
                                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                                    transformRequest: $scope.changeData
                                }).then(function successCallback(response) {
                                    console.log(response.data)
                                    if (response.data.status == 101) {
                                        $.alert({
                                            title: '验证',
                                            text: '验证成功',
                                            onOK: function () {
                                                //点击确认
                                                $scope.userDesc.phone=$scope.user_Phone;
                                                history.go(0);
                                            }
                                        });
                                    }else{
                                        $.alert({
                                            title: '验证',
                                            text: response.data.data,
                                           
                                        });  
                                    }
                                }, function errorCallback(response) {
                                    console.log(response)
                                    // 请求失败执行代码
                                });

                            } else {
                                $.alert({
                                    title: '验证',
                                    text: '验证失败,请重新输入',
                                    onOK: function () {
                                        //点击确认
                                    }
                                });
                            }

                            //点击确认
                        },
                        onCancel: function () {
                            //点击取消
                            //验证失败后的操作   
                            //   $.alert({
                            //     title: '标题',
                            //     text: '',
                            //     onOK: function () {
                            //       //点击确认
                            //     }
                            //   });
                        }
                    });
                }else{
                    $.alert({
                        title: '验证',
                        text: response.data.data.message,
                       
                    }); 
                }

            }, function errorCallback(response) {
                console.log(response)
                // 请求失败执行代码
            });

        } else {
            // 弹出框提示
            $.alert({
                title: '短信验证',
                text: '手机号码输入错误，请重新输入',
                onOK: function () {
                    //点击确认
                }
            });
        }


    }
    $scope.sendFile = function (type) {
        console.log(type)
        var url = $scope.httptit + '/index/user/verify.html',
            // var url = $scope.httptit+'/index/user/verify.html',
            file = $scope.fileToUpload;
            if(!$scope.fileToUpload){
                $.alert({
                    title: '提示',
                    text: '请选择上传照片',                
                });
            }
        console.log($scope.fileToUpload)
        type = type
        if (!file) return;
        fileUpload.uploadFileToUrl(file, url, type);
    }
    $("#CARE_Z").change(function () {
        var file = this.files[0];
        if (window.FileReader) {
            var reader = new FileReader();
            reader.readAsDataURL(file);
            //监听文件读取结束后事件    
            reader.onloadend = function (e) {
                $scope.$apply(function () {
                    $scope.CARE_Z = e.target.result.substring('/^data:image/(jpeg|png|gif);base64$/g');
                })
            };
        }
    });
    // 上传身份证反面
    $("#CARE_F").change(function () {
        var file = this.files[0];
        if (window.FileReader) {
            var reader = new FileReader();
            reader.readAsDataURL(file);
            //监听文件读取结束后事件    
            reader.onloadend = function (e) {
                $scope.$apply(function () {
                    $scope.CARE_F = e.target.result.substring('/^data:image/(jpeg|png|gif);base64$/g');
                })
            };
        }
    });



}]);
